﻿/**************************************************************
 * Project     : Titan Framework
 * Copyright (C) 2011 Microsoft Corp.
 * Author      : Julian Hu
 * Mail        : junhu@microsoft.com
 * Create Date : 2011-05-10
 * Summary     :   
 * 
 * 
 * Modified By : 
 * Date        : 
 * Mail        : 
 * Comment     :   
 * *************************************************************/

using System;
using System.Collections.Generic;
using System.Text;

namespace Titan.Logging
{
    /// <summary>
    /// 日志工厂基类.
    /// </summary>
    public abstract class LoggerAdapter : DisposableObject, ILoggerAdapter
    {

        #region Fields
        /// <summary>
        /// 用于存储不同Type的日志操作对象.
        /// </summary>
        private readonly Dictionary<Type, ILogger> _loggers = new Dictionary<Type, ILogger>();
        #endregion

        #region Disposal
        /// <summary>
        /// 资源释放.
        /// </summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && !IsDisposed)
            {
                DisposeDictionary(_loggers);
                _loggers.Clear();
            }

            base.Dispose(disposing);
        }
        #endregion

        #region Public Methods

        /// <summary>
        /// 根据不同的Type,获取具体的日志操作对象.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public ILogger GetLogger(Type type)
        {
            lock (_loggers)
            {
                if (_loggers.ContainsKey(type))
                    return _loggers[type];

                ILogger logger = CreateLogger(type);
                _loggers.Add(type, logger);

                return logger;
            }
        }
        #endregion

        #region Protected Methods
        protected abstract ILogger CreateLogger(Type type);
        #endregion

    }
}
